package com.zmj.framework.handler.exception;

import com.zmj.framework.response.ResponseCode;
import com.zmj.framework.response.Result;
import io.lettuce.core.RedisException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.sql.SQLException;
import java.sql.SQLNonTransientException;

@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MiddlewareExceptionControllerAdvice {

    @ExceptionHandler(SQLException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleSQLException(SQLException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.SQL_ERROR);
    }

    @ExceptionHandler(SQLNonTransientException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleSQLNonTransientException(SQLNonTransientException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.SQL_ERROR);
    }


    @ExceptionHandler(RedisException.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result<Void> handleRedisException(RedisException ex) {
        return ExceptionMapperUtil.buildBody(ex,ResponseCode.NO_SQL_ERROR);
    }

}
